package com.gxa.shop.servlet.order;

import com.alibaba.fastjson.JSON;

import com.gxa.shop.bean.dto.OrderDto;
import com.gxa.shop.bean.vo.AddressVo;
import com.gxa.shop.bean.vo.OrderVo;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.service.order.OrderService;
import com.gxa.shop.service.order.impl.OrderServiceImpl;
import com.gxa.shop.util.MyFormatUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@WebServlet(name = "SelectAllOrderServlet",value = "/order/findPage")
public class SelectAllOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, String[]> parameterMap = req.getParameterMap();
        //拷贝实体类
        OrderDto orderDto  = new OrderDto();


        try {
            BeanUtils.copyProperties(orderDto,parameterMap);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            System.out.println("参数匹配不成功");
        }

        OrderService orderService=new OrderServiceImpl();

        Long total=orderService.totalAllOrder(orderDto);
        R r;
        List<OrderVo> list = Collections.emptyList();
        if (total>0){
             list= orderService.selectOrder(orderDto);
        }
        r=list!=null?R.ok().addData("total",total).addData("list",list):R.ok("没有品牌信息");
        String listResult=JSON.toJSONString(r);
        System.out.println(listResult);
        PrintWriter writer=resp.getWriter();
        writer.write(listResult);
        writer.flush();
        writer.close();
    }
}
